<!DOCTYPE html>
<html lang="en" class="x-admin-sm" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>我要联盟</title>
    <header th:replace="header::html"></header>
</head>
<body>
<div class="demoTable">
    <div class="layui-fluid">
        <div class="layui-row  layui-col-space15">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-body ">
                        <div class="layui-inline">
                            联盟名称:
                            <div class="layui-inline">
                                <input class="layui-input" name="id" id="aname" autocomplete="off">
                            </div>
                        </div>
                        <div class="layui-inline layui-show-xs-block">
                            <button class="layui-btn" data-type="reload">查询</button>
                            <input type="button" id="add" class="layui-btn" value="创建联盟">
                            <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">

                        </div>
                    </div>
                    <div class="layui-card-body ">
                        <table id="demo" lay-filter="test"></table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<!--<div class="demoTable">-->
<!--    联盟名称:-->
<!--&lt;!&ndash;    <input  id="aname">&ndash;&gt;-->
<!--&lt;!&ndash;    <div class="layui-inline">&ndash;&gt;-->
<!--&lt;!&ndash;        <input class="layui-input" name="id" id="aname" autocomplete="off">&ndash;&gt;-->
<!--&lt;!&ndash;    </div>&ndash;&gt;-->
<!--    <button class="layui-btn" data-type="reload">查询</button>-->
<!--    <input type="button" id="add" class="layui-btn" value="创建联盟">-->
<!--    <input type="button" class="layui-btn layui-btn-danger layui-btn-xs" value="返回主页" onclick="back()">-->
<!--</div>-->

<!--<table id="demo" lay-filter="test"></table>-->

<script id="msg" type="text/html">
    <div class="layui-form-item" >
        <div class="layui-input-inline">
            联盟名称:<input type="text"  id="name" required  lay-verify="required" placeholder="请输入联盟名称" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            联盟简介:<input type="text"  id="ainfo" required  lay-verify="required" placeholder="请输入联盟简介" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            联盟账号:<input type="text"  id="aaccount" required  lay-verify="required" placeholder="请输入联盟账户" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            入会费用:<input type="text"  id="amoney" required  lay-verify="required" placeholder="请输入费用" autocomplete="off" class="layui-input">
        </div>
        <div class="layui-input-inline">
            联盟类型:<select  lay-verify="required" id="uastate">
        </select>
        </div>
    </div>
    <button type="button" class="layui-btn layui-btn-normal" id="addAlliance">确认新增</button>
</script>

<script type="text/html" id="bar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">我要加入</a>
</script>

<script>

	function back()
	{
		window.parent.location.href="/api-front/getPage?pageName=index";
	}

	layui.use('table', function(){
		var table = layui.table;
		table.render({
			elem: '#demo'
			,type:'POST'
			,url: '/api-front/alliance/getList'
			,page: true
			,limit:5
			,limits:[5]
			,id:"testReload"
			,cols: [
				[
					{field: 'aid', hide:true}
					,{field: 'aname', title: '联盟名称', width:200}
					,{field: 'ainfo', title: '联盟简介', width:200}
					,{field: 'tips3', title: '联盟类型', width:200}
					,{field: 'amargin', title: '入会费用', width:200}
					,{title:'操作',width:150,toolbar:"#bar"}
				]
			]
		});
		$('.demoTable .layui-btn').on('click', function ()
		{
			var type = $(this).data('type');
			if (type == 'reload')
			{
				//执行重载
				table.reload('testReload',
					{
						page:
							{
								curr: 1
							}
						, where:
							{
								aname: $("#aname").val()
							}
					});
			}
		});


		table.on('tool(test)', function(obj)
		{
			var data = obj.data;
			var layEvent = obj.event;
			var tr = obj.tr;
            layer.confirm('确定要申请加入该联盟吗?', function(index)
            {
                $.ajax({
                    type:"POST",
                    url:'/api-front/alliance/addAlliance',
                    dataType:"text",
                    data:{aid:data.aid,money:data.amargin},
                    success:function(msg)
                    {
                        if(msg==="yes")
                        {
	                        layer.open({
		                        title: ['温馨提示'],
		                        content: '申请成功，等待管理员审核',
		                        btn: ['确定'],
		                        closeBtn :0,
		                        yes: function(){
			                        window.location.href="/api-front/getPage?pageName=frontPage/alliance"
		                        }
	                        });
                        }
                        else if(msg==="noMoney")
                        {
                            layer.alert("余额不足，请充值", {icon: 5});
                        }
                        else
                        {
                            layer.alert("申请失败，请重试", {icon: 5});
                        }
                    },
                    error:function ()
                    {
                        layer.alert("服务器正忙.....", {icon: 5});
                    }
                });
            });
		});


		$("#add").click(function ()
		{
			$.ajax({
				type:"POST",
				url:"/api-back/type/getServer",
				success:function(msg)
				{
					var str=layer.open({
						type: 1,
						content: $('#msg').html(),
						area: ['300px'],
						title: '创建联盟',
						btn:['返回'],
						success:function ()
                        {
	                        document.getElementById("uastate").length=0;
	                        for(var i=0;i<msg.length;i++)
	                        {
		                        $('#uastate').append("<option value='"+msg[i].sid+"'>"+msg[i].sname+"</option>")
	                        }
						}
					});
					$("#addAlliance").click(function ()
					{
						var aname=document.getElementById("name").value;
						var ainfo=document.getElementById("ainfo").value;
						var aaccount=document.getElementById("aaccount").value;
						var amoney=document.getElementById("amoney").value;
						var uastate=document.getElementById("uastate");
						var v=uastate.options[uastate.selectedIndex].value;
						if(aname.length===0 || ainfo.length===0 || amoney.length===0 || aaccount.length===0)
						{
							layer.alert("请将内容填写完整",{icon:5});
						}
						else if(aname.length>15)
						{
							layer.alert("联盟名称不得超过15字", {icon: 5});
						}
						else if(ainfo.length>15)
						{
							layer.alert("联盟简介不得超过15字", {icon: 5});
						}
						else if(aaccount.length>10)
						{
							layer.alert("联盟账号长度上限为10", {icon: 5});
						}
						else if(!(amoney%1===0))
						{
							layer.alert("请输入整数费用", {icon: 5});
						}
						else if(!(amoney>0&&amoney<=1000))
						{
							layer.alert("请输入1-1000元的费用", {icon: 5});
						}
						else
						{

							$.ajax({
								type:"POST",
								url:"/api-front/alliance/checkAccount",
								dataType:"text",
								data:{aaccount:aaccount},
								success:function(msg)
								{
									if(msg==="yes")
									{
										$.ajax({
											type:"POST",
											url:"/api-front/alliance/newAlliance",
											dataType:"text",
											data:{aname:aname,ainfo:ainfo,amoney:amoney,aaccount:aaccount,uastate:v},
											success:function(msg)
											{
												if(msg==="yes")
												{
													layer.alert("恭喜你，创建成功", {icon: 1});
													layer.close(str);
												}
												else
												{
													layer.alert("创建失败，请重试", {icon: 5});
												}
											},
											error:function ()
											{
												layer.alert("服务器正忙.....", {icon: 5});
											}
										});
									}
									else
									{
										layer.alert("该联盟账户已存在", {icon: 5});
									}
								},
								error:function ()
								{
									layer.alert("服务器正忙.....", {icon: 5});
								}
							});
						}
					});
				},
				error:function ()
				{
					layer.alert("服务器正忙.....", {icon: 5});
				}
			});

		});
	});
</script>
</body>
</html>